<template>
  <el-dialog class='subject-status-add-record' :title="dialogTitle" width="600px" :show-close='true'
             :visible.sync="dialogVisible" :close-on-click-modal="false" :before-close="handleClose">
    <div>
      <el-form :rules="rules" ref="formRef" size="mini" :model="formData" label-width="140px">
        <el-form-item label="表：" prop='surface'>
          <el-input :disabled="dialogTitle === '编辑' || !isOperate" style="width: 80%" maxlength="50" show-word-limit clearable placeholder="请输入" v-model.trim="formData.surface">
          </el-input>
        </el-form-item>
        <el-form-item label="DOMAIN：">
          <el-input :disabled="!isOperate" style="width: 80%" clearable placeholder="请输入" maxlength="200" show-word-limit v-model="formData.domain">
          </el-input>
        </el-form-item>
        <el-form-item label="表名：" prop='surfaceName'>
          <el-input :disabled="!isOperate" style="width: 80%" clearable placeholder="请输入" maxlength="50" show-word-limit v-model="formData.surfaceName">
          </el-input>
        </el-form-item>
        <el-form-item label="分类：">
          <el-input :disabled="!isOperate" style="width: 80%" clearable placeholder="请输入" maxlength="200" show-word-limit v-model="formData.classify">
          </el-input>
          <el-dropdown placement="bottom-start">
            <el-button :disabled="!isOperate" icon="el-icon-more" style="margin-left: 8px" @click="handleGetClassifyList"></el-button>
            <el-dropdown-menu slot="dropdown" >
              <el-dropdown-item v-for="item in classifyList" :key="item.value">
                <span class="span-dropdown-item" @click="handleItemChecked(item)">{{ item }}</span>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-form-item>
        <el-form-item label="引用表：">
          <el-select v-model="formData.quoteCrfSurface" placeholder="请选择" style="width: 80%" clearable :disabled="!isOperate">
            <el-option v-for="item in referenceTableList" :key="item.surface" :label="item.surfaceName" :value="item.surface">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="handleClose">取 消</el-button>
      <el-button :loading='loading' size="small" type="primary" @click="handleSave">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { mapActions } from 'vuex'

export default {
  props: {
    editData: Object,
    dialogVisible: Boolean,
    dialogTitle: String,
    referenceTableList: Array,
    classifyList: Array,
    searchParams: Object,
    isOperate: Boolean
  },
  data() {
    return {
      rules: {
        surface: [
          { required: true, message: '请输入', trigger: 'blur' },
          { pattern: /^[a-zA-Z][a-zA-Z0-9_]*$/, message: '必须字母开头，由字母数字下划线组合', trigger: 'blur' }
        ],
        surfaceName: [{ required: true, message: '请输入', trigger: 'blur' }],
        embedded: [{ required: true, message: '请选择', trigger: 'blur' }]
      },
      loading: false,
      defaultFormData: {
        surface: '',
        domain: '',
        surfaceName: '',
        classify: '',
        quoteCrfSurface: '',
        embedded: '0'
      },
      formData: {}
    }
  },

  watch: {
    dialogVisible(val) {
      if (val) {
        this.formData = this.editData.id ? { ...this.editData } : { ...this.defaultFormData, ...this.editData }
      } else {
        this.formData = { ...this.defaultFormData }
      }
    },
  },

  methods: {
    ...mapActions('eCRFDesign', ['addCrfDesign', 'editCrfDesign']),

    // 关闭取消
    handleClose() {
      this.formData = { ...this.defaultFormData }
      this.$emit('handleCancel')
    },

    // 保存提交
    handleSave() {
      this.loading = true
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          const data = {
            ...this.formData,
            ...this.searchParams,
            embedded: this.formData.quoteCrfSurface ? '1' : '0' // 根据是否选择引用表判断是否是内嵌表
          }
          
          if (data.id) {
            this.editCrfDesign({ data }).then(res => {
              const { code } = res
              if (code === 200) {
                this.$message({
                  message: '保存成功',
                  type: 'success'
                })
                this.$refs.formRef.resetFields()
                this.$emit('handleCancel', 'refresh')
              }
              this.loading = false
            })
          } else {
            this.addCrfDesign({ data }).then(res => {
              const { code } = res
              if (code === 200) {
                this.$message({
                  message: '保存成功',
                  type: 'success'
                })
                this.$refs.formRef.resetFields()
                this.$emit('handleCancel', 'refresh')
              }
              this.loading = false
            })
          }
        } else {
          this.loading = false
          return false
        }
      })
    },
    // 查询分类选项
    handleGetClassifyList() {

    },

    // 分类条目选中事件
    handleItemChecked(text) {
      console.log('text', text)
      this.formData.classify = text
    }
  }
}
</script>
<style lang="scss" scoped>
@import './add-design.scss';
</style>
